#!/bin/sh

# Installing atomic_ops
# ---------------------


#if test $# -gt 1
#then
#    echo "Usage: $0 [atomic_ops-prefix]"
#    exit 1
#fi
exportUserspace=${PWD}
#atomicOpsVersion="1.1" 
atomicOpsVersion="1.2" 
atomicPrefix=${exportUserspace}/atomic_ops-${atomicOpsVersion}-prefix 
if test $# -gt 0
then
    atomicPrefix=$1
    if test ! -d $atomicPrefix
    then
        echo "Sorry I cannot see $atomicPrefix"
        exit 1
    fi
fi

# if already built, we can exit gracefully...
if test -e $atomicPrefix/include/atomic_ops.h -a -e $atomicPrefix/lib/libatomic_ops.a
then
    echo "atomic_ops already built previously to $atomicPrefix"
    exit 0
fi

# we need to build atomic_ops. Assume the source is in the forest.
       
atomicSrcPath=${PWD}/../../..
if test ! -d $atomicSrcPath/libatomic_ops-${atomicOpsVersion}
    then
# someone other than us may be building this with their own unique
#  directory structure, but they need to tell us stuff...
    echo "Which directory contains the atomic_ops source code directory libatomic_ops-${atomicOpsVersion}?"
    read atomicPath
    if test ! -d $atomicPath/libatomic_ops-${atomicOpsVersion} 
        then
        echo "Sorry I cannot see $atomicPath/libatomic_ops-${atomicOpsVersion}"
        exit 1
    fi
    atomicSrcPath=$atomicPath 
fi

# ok we found atomic_ops-${atomicOpsVersion}
echo "Building libatomic_ops-${atomicOpsVersion}..."
cd $atomicSrcPath/libatomic_ops-${atomicOpsVersion} 
#autoconf
./configure --prefix=$atomicPrefix
make
make install


# soft-link items in prefix/lib to export_userspace/netinet/user_lib and 
#  from prefix/include to export_userspace/netinet/user_include

cd $exportUserspace
if test ! -d user_lib
    then
    mkdir user_lib
fi
cd user_lib
ln -s $atomicPrefix/lib/libatomic_ops.a
ln -s $atomicPrefix/lib/libatomic_ops_gpl.a
cd $exportUserspace/user_include
for f in $atomicPrefix/include/* ; do
ln -s $f
done



